package com.example.demo.controller;

import com.example.demo.dto.HealthDTO;
import com.example.demo.dto.UserDTO;
import com.example.demo.entity.*;
import com.example.demo.enums.ResultEnum;
import com.example.demo.service.OldManService;
import com.example.demo.utils.EntityToDTO;
import com.example.demo.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/oldman")
@Controller
public class OldManController {
    @Value("${web.upload-path}")
    private String uploadPath;

    @Autowired
    private OldManService oldManService;

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);


    @RequestMapping("/findAllOldman")
    @ResponseBody
    public ResultVO findAllOldman(){
        List<OldMan> lists =  oldManService.findAllOldman();
        if (lists.isEmpty()) {
            return new ResultVO(ResultEnum.DATA_NULL);
        }
        return new ResultVO(ResultEnum.SUCCESS,lists);
    }

    @RequestMapping("/search")
    @ResponseBody
    public ResultVO search(String val){
        List<OldMan> lists = oldManService.search(val);
        return new ResultVO(ResultEnum.SUCCESS,lists);
    }

    @RequestMapping("/addOldman")
    @ResponseBody
    public ResultVO addOldman( @RequestPart("oimage") MultipartFile file, HttpServletRequest request, @RequestParam Map<String,String> map) throws IOException {

        String imgPath = "";
        if (!file.isEmpty() || file != null){
            String canonicalPath = new File(uploadPath).getCanonicalPath();
            String filename = UUID.randomUUID() + file.getOriginalFilename();
            imgPath = canonicalPath + "\\" + filename;
            File dest = new File(imgPath);
            String path1 = uploadPath.substring(0,uploadPath.indexOf(":")+1);
            imgPath = imgPath.substring(imgPath.lastIndexOf(path1)+2);
            map.put("imgPath",imgPath);
            try{
                file.transferTo(dest);
                LOGGER.info("上传成功");
            } catch (IOException e){
                LOGGER.error(e.toString(), e);
            }
        }

        if (map.get("oid") != null || map.get("oid") != ""){
            int result = oldManService.updateOldman(map);
            return new ResultVO(ResultEnum.SUCCESS,result);
        }
        int result = oldManService.addOldman(map);
        return new ResultVO(ResultEnum.SUCCESS,result);
    }


    @RequestMapping("/findBedType")
    @ResponseBody
    public ResultVO findBedType(){
        List<BedType> lists = oldManService.findBedType();
        return new ResultVO(ResultEnum.SUCCESS,lists);
    }

    @RequestMapping("/findNationType")
    @ResponseBody
    public ResultVO findNationType(){
        List<Nation> lists = oldManService.findNationType();
        return new ResultVO(ResultEnum.SUCCESS,lists);
    }

    @RequestMapping("/addHealth")
    @ResponseBody
    public ResultVO addHealth(@RequestParam Map<String,String> map){
        if (map.get("hid") != null || map.get("hid") != ""){
            int result = oldManService.updateHealth(map);
            return new ResultVO(ResultEnum.SUCCESS,result);
        }
        int result = oldManService.addHealth(map);
        return new ResultVO(ResultEnum.SUCCESS,result);
    }

    @RequestMapping("/delHealthById")
    @ResponseBody
    public ResultVO delHealthById(Integer hid){
        int result = oldManService.delHealthById(hid);
        return new ResultVO(ResultEnum.SUCCESS,result);
    }
    @RequestMapping("/findTotal")
    @ResponseBody
    public ResultVO findTotal(){
        return new ResultVO(ResultEnum.SUCCESS,oldManService.findTotal());
    }
    @RequestMapping("/findAllHealth")
    @ResponseBody
    public ResultVO findAllHealth(int startNum,int rows){
        List<HealthDTO> lists = oldManService.findAllHealth(startNum,rows);
        return new ResultVO(ResultEnum.SUCCESS,lists);
    }

    @RequestMapping("/showToPage")
    @ResponseBody
    public ResultVO showToPage(int startNum,int rows){
        List<HealthDTO> lists = oldManService.showToPage(startNum,rows);
        if (!lists.isEmpty()){
            return new ResultVO(ResultEnum.SUCCESS,lists);
        }
        return  new ResultVO(ResultEnum.DATA_NULL);
    }
}
